package com.leon.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.Map;


@Controller
@RequestMapping("/demo")
public class DemoController {

    /**
     * 原生 Servlet 的支持：handle02?id=123
     * 原始方法，不推荐
     */
    @RequestMapping("/handle02")
    public ModelAndView handle02(HttpServletRequest request, HttpServletResponse response, HttpSession session){

        String id = request.getParameter("id");

        ModelAndView mv = new ModelAndView();
        mv.addObject("date", id);
        mv.setViewName("success");

        return mv;
    }



    /**
     * 通过自定义转化器接收指定格式的日期参数。url ：/handler05?date=2019-12-01
     */
    @RequestMapping("/handler05")
    public String handler05(Date date){
        System.out.println(date);
        return "";
    }

    /**
     *  REST 风格支持，URI ：handler06/15
     */
    @RequestMapping(value = "/handler06/{id}", method = {RequestMethod.GET})
    public String handler06(@PathVariable("id")Integer id){

        System.out.println(id);
        return null;
    }

    /**
     * REST 风格，POST URI ： /handler07  参数在 http body 中
     * 和普通的请求参数处理没有差别
     */
    @RequestMapping(value = "/handler07", method = {RequestMethod.POST})
    public String handler07(String username){
        System.out.println(username);
        return null;
    }

    /**
     * RESTful 风格  put uri：  /demo/handle/15/lisi
     */
    @RequestMapping(value = "/handle/{id}/{name}",method = {RequestMethod.PUT})
    public String handlePut(@PathVariable("id") Integer id,@PathVariable("name") String username) {

        System.out.println(id + username);
        return null;
    }


    /*
     * restful 风格 delete uri:  /demo/handle/15
     */
    @RequestMapping(value = "/handle/{id}",method = {RequestMethod.DELETE})
    public String handleDelete(@PathVariable("id") Integer id) {
        System.out.println(id);
        return "modelAndView";
    }
}
